//
//  LQComposeToolBar.swift
//  Weibo
//
//  Created by ios on 16/8/16.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
//  toolbar按钮的枚举类型
enum LQComposeToolBarButtonType: Int {
    //  图片
    case Picture = 0
    //  @
    case Mention = 1
    //  #
    case Trend = 2
    //  表情
    case Emoticon = 3
    //  加号
    case Add = 4
}

class LQComposeToolBar: UIStackView {
        //  点击按钮需要执行的闭包
        var didSelecteToolBarButtonClosure: ((type: LQComposeToolBarButtonType)->())?
        //表情按钮
    var emoticonButton: UIButton?
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupUI()
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        //  添加控件设置布局方式
        private func setupUI() {
            //  水平方向布局
            axis = .Horizontal
            //  子控件等比填充
            distribution = .FillEqually
            //  创建按钮
            addChildButton("compose_toolbar_picture", type: .Picture)
            addChildButton("compose_mentionbutton_background", type: .Mention)
            addChildButton("compose_trendbutton_background", type: .Trend)
          emoticonButton =  addChildButton("compose_emoticonbutton_background", type: .Emoticon)
            addChildButton("compose_add_background", type: .Add)
        }
        
        //  创建子按钮的通用方法
        private func addChildButton(imageName: String, type: LQComposeToolBarButtonType) -> UIButton {
            let button = UIButton()
            //  根据枚举的原始值,作为按钮的tag
            button.tag = type.rawValue
            
            //  添加点击事件
            button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
            
            //  设置图片
            button.setImage(UIImage(named: imageName), forState: .Normal)
            button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
            
            //  设置背景图片
            button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
            
            //button.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
            
            
            //  取消高亮效果
            button.adjustsImageWhenHighlighted = false
            
            //  添加按钮
            addArrangedSubview(button)
            
            return button
            
        }
        
        
        //  MARK:   - 点击事件
        
        @objc private func buttonAction(button: UIButton) {
            //  通过原始值获取枚举
            let type = LQComposeToolBarButtonType(rawValue: button.tag)!
            
            //  执行闭包,传入参数(枚举)
            didSelecteToolBarButtonClosure?(type: type)
        }
    
     //  根据外界传入的键盘类型设置不同的icon
    
    func switchEmotionIcon(isEmoticon: Bool){
        if isEmoticon {
            //显示系统键盘
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
        
        }else{
           //显示表情键盘
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
